/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.context.browser.BrowserContainer;
import fever.events.ApplicationEvent;
import fever.Fever;
import fever.log.FeverDebug;
import fever.utils.FireBugTracer;

import flash.external.ExternalInterface;

/**
 * Browser container properties.
 * 
 * <p>If {@link fever.log.FeverDebug#isOn} is {@code true} and application run on 
 * FireFox browser client, connect the 
 * {@link fever.utils.FireBugTracer} ( Firebug console extension} to channel.
 * 
 * @author Romain Ecarnot
 */
class fever.app.context.browser.ClientBrowser
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _container : BrowserContainer;	private var _ie : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function ClientBrowser( container : BrowserContainer )
	{
		_container = container;
		
		_checkIE();
		_initEventFlow();
	}
	
	/**
	 * Returns {@code true} if application running under IE browser.
	 * 
	 * <p>Note that {@code ExternalInterface} must be available to retreive 
	 * the result, otherwise return {@code null}.
	 */
	public function isIE() : Boolean
	{
		return _ie;
	}
		
	/**
	 * Returns player string version
	 */
	public function toString() : String
	{
		return "";
	}	
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _checkIE() : Void
	{
		if( ExternalInterface.available )
		{
			var f : String = "";
			f += "function getBrowserType( ){";
			f += "if ( navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length ) {";
			f += "return false;";
			f += "} else return true;";
			f += "}";
			
			ExternalInterface.call( "eval", f );
			_ie = Boolean( ExternalInterface.call( "getBrowserType" ) );
		}
		else
		{
			_ie = null;
			FeverDebug.ERROR( "External interface is not available in current context" );
		}
	}
	
	private function _initEventFlow() : Void
	{
		var test : String = "function checkFirefox() { if (navigator.userAgent.indexOf(\"Firefox\") > 0) return true;else return false;}";
		ExternalInterface.call( "eval", test );
		var force : Boolean = Boolean( ExternalInterface.call( "checkFirefox" ) );
		
		if( force && FeverDebug.isOn )
			FireBugTracer.connect( FeverDebug.channel );
			
		var f : String = "";
		f = "function getFeverMovie( id ) { " +
		"if (navigator.appName.indexOf(\"Microsoft\") != -1) {" +
		"return window[id]" +
		"} else {" +
		"return document[id]" +
		"}" +
		"}";
		f += "function listenResize(){ getFeverMovie( \"fswf\" )._onResize();}";
		f += "function listenFocus(){ getFeverMovie( \"fswf\" )._onFocus();}";
		f += "function listenBlur(){ getFeverMovie( \"fswf\" )._onBlur();}";
		
		if( force )
		{
			f += "function addFeverListeners(){" +
			"window.onresize = listenResize;" +
			"document.body.onblur = listenBlur;" +
			"document.body.onfocus = listenFocus;" +
			"}";
		}
		else
		{
			f += "function addFeverListeners(){" +
			"window.onresize = listenResize;" +
			"window.onblur = listenBlur;" +
			"window.onfocus = listenFocus;" +
			"}";
		}
		ExternalInterface.call( "eval", f );
		ExternalInterface.call( "addFeverListeners" );
		
		var b : Boolean;
		b = ExternalInterface.addCallback( "_onResize", this, _onResize );		b = ExternalInterface.addCallback( "_onBlur", this, _onBlur );		b = ExternalInterface.addCallback( "_onFocus", this, _onFocus );
	}
	
	private function _onResize() : Void
	{
		Fever.application.broadcastEvent( 
			new ApplicationEvent( 
				ApplicationEvent.onResizeEVENT,  
				_container.getSize() 
			) 
		);
	}
	
	private function _onBlur() : Void
	{
		Fever.application.broadcastEvent( 
			new ApplicationEvent( 
				ApplicationEvent.onFocusChangeEVENT,  
				false
			) 
		);
	}
	
	private function _onFocus() : Void
	{
		Fever.application.broadcastEvent( 
			new ApplicationEvent( 
				ApplicationEvent.onFocusChangeEVENT,  
				true
			) 
		);
	}
}